package com.xs.demo.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xs.demo.service.LoginService;
import com.xs.demo.util.StringUtil;
/**
 * 
 * @author 宗潇帅
 * 类名称：LoginController 
 * @date 2014-11-17 下午3:56:17 
 * 备注：
 */
@Controller
@RequestMapping(value="/system")
public class LoginController {
	
	private static Log log = LogFactory.getLog(LoginController.class);
	
	LoginService loginService;
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/weblogin")
	public String weblogin(HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		String username = ServletRequestUtils.getStringParameter(request, "username","");
		String password = ServletRequestUtils.getStringParameter(request, "password","");
		String result = "";
		request.setAttribute("username", username);
		try {
			result = loginService.webLogin(username,password,request);
		} catch (Exception e) {
			result = "{\"success\":false,\"msg\":\"系统错误，请稍候再试！\"}";
			if(log.isErrorEnabled()){
				log.error("error", e);
			}
		}
		StringUtil.writeToWeb(result, "html", response);
		return null;
	}
	@RequestMapping(value="/logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.getSession().invalidate();
		response.sendRedirect(request.getContextPath()+"/system/login.jsp");
		return null;
	}
	
	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	
}
